import { useAtomValue } from "jotai";
import { Navigate } from "react-router";
import type { PropsWithChildren } from "react";
import { isAuthPendingAtom, isLoginAtom } from "@/store/user";
import { Spin } from "antd";

export default function Authenticated({ children }: PropsWithChildren) {
  const isAuthPending = useAtomValue(isAuthPendingAtom);
  const isLogin = useAtomValue(isLoginAtom);

  if (isAuthPending) {
    return (
      <div className="flex justify-center pt-36">
        <Spin size="large" />
      </div>
    );
  }

  if (!isLogin) {
    return <Navigate to="/login" replace />;
  }

  return children;
}
